<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 2018/11/19
 * Time: 10:36
 */

namespace common\members\wodrow\tools;


use widewhale\debug\vardumper\components\VarDumper;

class Tools extends \common\components\tools\Tools
{
    /**
     * xml转换数组
     * @param $xml
     * @return mixed
     */
    public static function xmlToArray($xml)
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $values;
    }

    /*
     * 生成随机字符串
     * @param int $length 生成随机字符串的长度
     * @param string $char 组成随机字符串的字符串
     * @return string $string 生成的随机字符串
     */
    public static function strRand($length = 32, $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_') {
        if(!is_int($length) || $length < 0) {
            return false;
        }
        $string = '';
        for($i = $length; $i > 0; $i--) {
            $string .= $char[mt_rand(0, strlen($char) - 1)];
        }

        return $string;
    }

    /**
     * debug工具栏输出变量
     * @param $dump
     */
    public static function yiiDebugDump($dump)
    {
        VarDumper::dump($dump);
    }
}